package cn.edu.sgu.www.mhxysy.service.qiling.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.qiling.QilingCategoryDTO;
import cn.edu.sgu.www.mhxysy.entity.equipment.EquipmentCategory;
import cn.edu.sgu.www.mhxysy.entity.qiling.QilingCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.equipment.EquipmentCategoryMapper;
import cn.edu.sgu.www.mhxysy.mapper.qiling.QilingCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.qiling.QilingCategoryPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.qiling.QilingCategoryService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class QilingCategoryServiceImpl implements QilingCategoryService {

	private final UploadUtils uploadUtils;
	private final ImageUpdater imageUpdater;
	private final QilingCategoryMapper qilingCategoryMapper;
	private final EquipmentCategoryMapper equipmentCategoryMapper;

	@Autowired
	public QilingCategoryServiceImpl(
			UploadUtils uploadUtils,
			ImageUpdater imageUpdater,
			QilingCategoryMapper qilingCategoryMapper,
			EquipmentCategoryMapper equipmentCategoryMapper) {
		this.uploadUtils = uploadUtils;
		this.imageUpdater = imageUpdater;
		this.qilingCategoryMapper = qilingCategoryMapper;
		this.equipmentCategoryMapper = equipmentCategoryMapper;
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = CustomDirectories.DIRECTORY_ZBQL;

		return uploadUtils.upload(file, directory);
	}

	@Override
	public void insert(QilingCategoryDTO qilingCategoryDTO) {
		EquipmentCategory equipmentCategory = equipmentCategoryMapper.selectById(qilingCategoryDTO.getEquipmentId());

		if (equipmentCategory.getGrade() < 60) {
			throw new GlobalException(ResponseCode.FORBIDDEN, "60级以下装备未开通器灵功能。");
		}

		QilingCategory qilingCategory = toEntity(qilingCategoryDTO);

		String note = "#{name}的器灵，可镶嵌融入#{name}，发挥#{name}更强的内在潜能。";

		qilingCategory.setId(null);
		qilingCategory.setNote(note.replaceAll("#\\{name}", equipmentCategory.getName()));

		qilingCategoryMapper.insert(qilingCategory);
	}

	@Override
	public void updateById(QilingCategoryDTO qilingCategoryDTO) {
		EquipmentCategory equipmentCategory = equipmentCategoryMapper.selectById(qilingCategoryDTO.getEquipmentId());

		if (equipmentCategory.getGrade() < 60) {
			throw new GlobalException(ResponseCode.FORBIDDEN, "60级以下装备未开通器灵功能~");
		}

		ObjectUtils.requireNonNull(qilingCategoryDTO.getId(), CustomMessages.ID_NOT_NULL);

		QilingCategory qilingCategory = toEntity(qilingCategoryDTO);

		qilingCategoryMapper.updateById(qilingCategory);
	}

	@Override
	public void updateImageById(Integer id, String image) {
		imageUpdater.updateImageById(image, new Consumer<Object>() {
			@Override
			public void accept(Object o) {
				UpdateWrapper<QilingCategory> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

				qilingCategoryMapper.update(updateWrapper.getEntity(), updateWrapper);
			}
		});
	}

	@Override
	public List<QilingCategory> selectAll() {
		return qilingCategoryMapper.selectList(null);
	}

	@Override
	public QilingCategory selectById(Integer id) {
		return qilingCategoryMapper.selectById(id);
	}

	@Override
	public Page<QilingCategory> selectByPage(QilingCategoryPager pager) {
		QueryWrapper<QilingCategory> queryWrapper = new QueryWrapper<>();
		Page<QilingCategory> page = Pager.ofPage(pager);

		queryWrapper.eq (
				pager.getGrade() != null,
				"grade", pager.getGrade()
		);
		queryWrapper.eq (
				pager.getEquipmentId() != null,
				"equipment_id", pager.getEquipmentId()
		);

		return qilingCategoryMapper.selectPage(page, queryWrapper);
	}

	private QilingCategory toEntity(QilingCategoryDTO qilingCategoryDTO) {
		QilingCategory qilingCategory = new QilingCategory();

		qilingCategory.setId(qilingCategoryDTO.getId());
		qilingCategory.setName(qilingCategoryDTO.getName());
		qilingCategory.setType(qilingCategoryDTO.getType());
		qilingCategory.setNote(qilingCategoryDTO.getNote());
		qilingCategory.setGrade(qilingCategoryDTO.getGrade());
		qilingCategory.setEquipmentId(qilingCategoryDTO.getEquipmentId());

		return qilingCategory;
	}

}